package com.simba.server.components;


import com.simba.common.command.ICommand;
import com.simba.common.command.SQLCommandExecutor;
import com.simba.common.command.WebCommandExecutor;
import com.simba.common.log.LogUtil;
import com.simba.server.commands.CommandCreator;
import org.apache.log4j.Logger;

public class CommandExecutor {
    private CommandExecutor(){}
    private static final Logger logger = Logger.getLogger(CommandExecutor.class);
    public static boolean executorSqlCommand(String commandName, Object params, boolean log)
    {
        long beginTime = System.currentTimeMillis();

        ICommand command = CommandCreator.getInstance().createCommand(commandName, params);
        boolean flag = SQLCommandExecutor.getInstance().executeCmd(command);

        long endTime = System.currentTimeMillis();
        long diff = endTime-beginTime;

        if (log||diff>3000){
            LogUtil.LogInfo(logger, "["+commandName+"] 命令执行！ 结果："+flag+", 耗时："+diff+" ms");
        }
        return flag;
    }
    public static boolean executorWebCommand(String commandName, Object params, boolean log)
    {
        long beginTime = System.currentTimeMillis();

        ICommand command = CommandCreator.getInstance().createCommand(commandName, params);
        boolean flag = WebCommandExecutor.getInstance().executeCmd(command);

        long endTime = System.currentTimeMillis();
        long diff = endTime-beginTime;

        if (log||diff>3000){
            LogUtil.LogInfo(logger, "["+commandName+"] 命令执行！ 结果："+flag+", 耗时："+diff+" ms");
        }
        return flag;
    }
}
